package com.sky.controller.user;

import com.sky.constant.MessageConstant;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderReportVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController("userOrderController")
@RequestMapping("/user/order")
@Api(tags = "C端订单相关接口")
public class OrderController {

    private final OrderService orderService;

    @Autowired
    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }

    /**
     * 用户下单
     *
     * @param submitDTO dto
     * @return vo
     */
    @PostMapping("/submit")
    @ApiOperation("用户下单")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO submitDTO) {
        log.info("用户下单啦：{}", submitDTO);
        return Result.success(orderService.submitOrder(submitDTO), MessageConstant.OPERATE_OK);
    }


    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }


    /**
     * 查询订单详情
     *
     * @param id 订单ID
     * @return 订单详情
     */
    @GetMapping("/orderDetail/{id}")
    @ApiOperation("查询订单详情")
    public Result<OrderVO> details(@PathVariable Long id) {
        log.info("查询订单详情：{}", id);
        return Result.success(orderService.detail(id), MessageConstant.SELECT_OK);
    }

    /**
     * 历史订单查询
     *
     * @param page     页码
     * @param pageSize 每页条数
     * @param status   订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消
     * @return 分页后的订单详情
     */
    @GetMapping("/historyOrders")
    @ApiOperation("历史订单查询")
    public Result<PageResult<OrderVO>> page(int page, int pageSize, Integer status) {
        log.info("历史订单查询:{},{},{}", page, pageSize, status);
        return Result.success(orderService.pageQuery4User(page, pageSize, status), MessageConstant.SELECT_OK);
    }


    /**
     * 取消订单
     *
     * @param id 订单ID
     * @return 提示
     */
    @PutMapping("/cancel/{id}")
    @ApiOperation("取消订单")
    public Result<String> cancel(@PathVariable Long id) {
        log.info("取消订单:{}", id);
        orderService.userCancelById(id);
        return Result.success(MessageConstant.OPERATE_OK);
    }


    /**
     * 用户再来一单
     *
     * @param id 订单ID
     * @return 提示
     */
    @PostMapping("/repetition/{id}")
    @ApiOperation("再来一单")
    public Result<String> anotherOrder(@PathVariable Long id) {
        log.info("用户再来一单:{}", id);
        orderService.anotherOrder(id);
        return Result.success(MessageConstant.OPERATE_OK);
    }

    /**
     * 用户催单
     *
     * @param id 订单ID
     * @return 提示
     */
    @GetMapping("/reminder/{id}")
    @ApiOperation("用户催单")
    public Result<String> reminder(@PathVariable Long id) {
        log.info("用户催单:{}", id);
        orderService.reminder(id);
        return Result.success(MessageConstant.OPERATE_OK);
    }
}
